package cn.ulc.socket.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.model.param.PaginationParam;
import cn.ulc.common.util.Result;
import cn.ulc.socket.admin.model.param.advertising.AdvertisingParam;
import cn.ulc.socket.admin.service.marketing.AdvertisingServ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

/*
*
* gen controller 2019-06-16
*/
@Validated
@RestController
@RequestMapping("/advertising")
public class AdvertisingCtrl {
    private static final Log log = LogFactory.get();

    @Autowired
    AdvertisingServ advertisingServ;

    @GetMapping
    public Result findList(String key,String startDate,String endDate, PaginationParam pager) {
        return Result.success(advertisingServ.findList(key,startDate,endDate, pager));
    }

    @GetMapping(value = "/{id}")
    public Result findDetail (@PathVariable @Min(1) Long id) {
        return Result.success(advertisingServ.findDetail(id));
    }

    @PostMapping
    public Result save(@Validated @RequestBody AdvertisingParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存广告位详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        return Result.success(advertisingServ.saveOne(param));
    }
    @PutMapping("/{id}")
    public Result update(@PathVariable("id") Long id, @RequestBody AdvertisingParam param, BindingResult result){
        if(result.hasErrors()) {
            log.error("保存广告位详情参数错误",result.getFieldError().getDefaultMessage());
            return Result.fail(result.getFieldError().getDefaultMessage());
        }
        param.setId(id);
        return Result.success(advertisingServ.saveOne(param));
    }
    @DeleteMapping("/{id}")
    public Result deleted(@PathVariable("id") Long id){
        return Result.success(advertisingServ.deleteOne(id));
    }

    @PutMapping("/changeStatus/{id}")
    public Result changeStatus(@PathVariable("id") Long id, AvailableStatus status){
        return Result.success(advertisingServ.changeStatus(id,status));
    }
}
